package com.shizhanzhe.zxbapp.task;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.NEW;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.customtask.CreditCardDetailsActivity;
import com.shizhanzhe.zxbapp.customtask.CreditTaskDetailsActivity;
import com.shizhanzhe.zxbapp.pay.PayVIPActivity;
import com.shizhanzhe.zxbapp.task.newshare.ShareWebActivity;
import com.shizhanzhe.zxbapp.ui.NewAdapter;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.widget.VipDIalog;


import java.util.List;

import static com.mob.MobSDK.getContext;

/**
 * Created by zz9527 on 2018/10/25.
 */
public class FastTaskActivity extends Activity {
    private ListView lv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hightask);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.colorAccent)
                .init();
        findViewById(R.id.backarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        TextView title = findViewById(R.id.top_title);
        title.setText("快速任务");
        SmartRefreshLayout refresh = findViewById(R.id.refresh);
        refresh.setRefreshHeader(new MaterialHeader(this));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                getData();
            }
        });
        lv = findViewById(R.id.lv_high);
        getData();
    }

    private void getData() {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.VIPFAST(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                try {
                    Gson gson = new Gson();
                    final List<NEW.DataBean> data = gson.fromJson(json, NEW.class).getData();
                    NewAdapter newAdapter = new NewAdapter(FastTaskActivity.this, data);
                    lv.setAdapter(newAdapter);
                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            if (UserBean.level == 0) {
                                final VipDIalog vipDIalog = new VipDIalog(FastTaskActivity.this);
                                vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                                    @Override
                                    public void chooseTrue() {
                                        startActivity(new Intent(getContext(), PayVIPActivity.class));
                                        vipDIalog.dismiss();
                                    }

                                    @Override
                                    public void chooseFalse() {
                                        vipDIalog.dismiss();

                                    }
                                });
                                vipDIalog.show();
                                vipDIalog.setContent();
                            } else {

                                NEW.DataBean dataBean = data.get(i);
                                Intent intent = new Intent();
                                switch (dataBean.getFclass()) {
                                    case 1://分享
//                                    intent.setClass(FastTaskActivity.this, WebActivity.class);
//                                    intent.putExtra("url", dataBean.getUrl());
//                                    intent.putExtra("shid", dataBean.getId());
//                                    intent.putExtra("fen", dataBean.getFen());
//                                    intent.putExtra("title", dataBean.getTitle());
//                                    intent.putExtra("img", dataBean.getLogo());
//                                    intent.putExtra("type", 0);
//                                    intent.putExtra("intro", dataBean.getShcon());
//                                    intent.putExtra("isshow", dataBean.getIsshow());
//                                    intent.putExtra("scon", dataBean.getShowtext());
//                                    intent.putExtra("ym", dataBean.getYuming());
                                        intent.setClass(FastTaskActivity.this, ShareWebActivity.class).putExtra("id", dataBean.getId());
                                        break;
                                    case 2://关注
                                        intent.setClass(FastTaskActivity.this, FollowActivity.class);
                                        intent.putExtra("iv", dataBean.getLogo());
                                        intent.putExtra("title", dataBean.getTitle());
                                        intent.putExtra("one", dataBean.getDan());
                                        intent.putExtra("all", dataBean.getMoney());
                                        intent.putExtra("wechatnum", dataBean.getWxunid());
                                        intent.putExtra("num", dataBean.getNum() + "");
                                        intent.putExtra("sum", dataBean.getSum() + "");
                                        if (dataBean.getSex().contains("男")) {
                                            intent.putExtra("sex", 1);
                                        } else if (dataBean.getSex().contains("女")) {
                                            intent.putExtra("sex", 2);
                                        }
                                        break;
                                    case 3://信用卡
                                        intent.setClass(FastTaskActivity.this, CreditCardDetailsActivity.class);
                                        intent.putExtra("taskid", dataBean.getId());
                                        break;
                                    case 4://截图任务
                                        intent.setClass(FastTaskActivity.this, CreditTaskDetailsActivity.class);
                                        intent.putExtra("taskid", dataBean.getId());
                                        break;
                                    default:
                                        break;
                                }
                                startActivity(intent);
                            }
                        }
                    });
                } catch (Exception e) {
                }
            }

        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
